/*
* 私募管理人 model
*privateManager.js*/

import {routerRedux} from 'dva/router';
import api from '../api';
import { notification } from 'antd';
import Mock from 'mockjs';
notification.config({
	placement: 'topRight',
	duration: 5,
});
export default {
	namespace : 'privateMger',
	state : {
		privateName:'',//私募名称
		linkNameId:'',//联系人
		isShowPdt:false,//展示产品
		investStrategy:[],//投资策略
		BOfficeCode:'',//分公司名称
		BDepartmentName:'',//营业部名称
		BOfficeId:'',//分公司id
	},
	subscriptions : {
		setup({dispatch, history}) {
			history.listen(({pathname, query}) => {
				if (pathname === '/match/matchManager') {
					dispatch({
						type: 'getsmmcAlxr',
						payload:{
							currentpage:1,
							pagesize:15,
						}
					})
					dispatch({
						type:'getDepartment',
						payload:{
							'flag':false
						}
					})
				}
			})
		}
	},
	effects : {
		*alertError({payload}){
			console.log('>>>',payload)
			const openNotificationWithIcon = (type) => {
			  notification[type]({
			    message: payload.msg,
			  });
			};
			openNotificationWithIcon('error')
		},
		*getDepartment({payload},{call,put,select}){
			const response = yield call(api.privateMgt.getDepartment, payload);
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			yield put({
        type: 'getDepartmentSuccess',
        payload: { response },
      });
		},
		*getsmmcAlxr({payload},{call,put,select}){
			const state=yield select(state => state.privateMger);
			console.log(1987,state)
			if(state.BOfficeId !==''&&state.BDepartmentName==''){
				payload.departmentId = state.BOfficeId;
			}else if(state.BOfficeId !=='' &&state.BDepartmentName!==''){
				payload.departmentId = state.BDepartmentName;
			}
			const response = yield call(api.privateMgt.getsmmcAlxr, payload);
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			yield put({
        type: 'getsmmcAlxrSuccess',
        payload: { response },
      });
		},
		*addPdtManager({payload},{call,put}){
			const value = payload.values;
			const response = yield call(api.privateMgt.addPdtManager, {...value});
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			yield put({
        type: 'getsmmcAlxr',
        payload:{
					currentpage:1,
					pagesize:15,
				}
      });
		},
		*editPdtManager({payload},{call,put}){
			const response = yield call(api.privateMgt.editPdtManager, payload);
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			yield put({
        type: 'getsmmcAlxr',
        payload:{
					currentpage:1,
					pagesize:15,
				}
      });
		},
		*deleteManager({payload},{call,put}){
			const id = payload.id
			const response = yield call(api.privateMgt.deleteManager, id);
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			yield put({
        type: 'getsmmcAlxr',
        payload:{
					currentpage:1,
					pagesize:15,
				}
      });
		},
		*deletePrivatePdt({payload},{call,put}){
			const productId = payload.id
			const response = yield call(api.privateMgt.deletePrivatePdt, productId);
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			yield put({
        type: 'getsmmcAlxr',
        payload:{
					currentpage:1,
					pagesize:15,
				}
      });
		},
		*addPdt({payload},{call,put}){
			const response = yield call(api.privateMgt.addPrivatePdt, payload);
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			yield put({
        type: 'getsmmcAlxr',
        payload:{
					currentpage:1,
					pagesize:15,
				}
      });
    },
    *editPrivatPdt({payload},{call,put}){
			// console.log('dddddddd',payload)
			const productId = payload.productId;
			const query={};
			query.investStrategy = payload.investStrategy;
			query.productCode = payload.productCode;
			query.productName = payload.productName;
			query.recordCode = payload.recordCode;
			const response = yield call(api.privateMgt.editPrivatPdt,productId,{...query});
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			yield put({
        type: 'getsmmcAlxr',
        payload:{
					currentpage:1,
					pagesize:15,
				}
      });
		},
    *fundCompetition({payload},{call,put}){
			const response = yield call(api.privateMgt.fundCompetition, payload);
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			yield put({
        type: 'fundCompetitionSuccess',
        payload: { response },
      });
		},
		*getDetails({payload},{call,put}){
			// console.log('pppppp',payload)
			const id = payload.id
			const response = yield call(api.privateMgt.getDetails, id);
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			yield put({
        type: 'getDetailsSuccess',
        payload: { response },
      });
		},
		*gameSignUp({payload},{call,put}){
			const competitionId = payload.competitionId;
			const response = yield call(api.privateMgt.gameSignUp,competitionId,{...payload})
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
		},
		
	},
	reducers : {
		setState(state, {payload}) {
			return {
				...state,
				...payload
			}
		},
		getDepartmentSuccess(state,{payload}){
			const {data} = payload.response
      console.log("getdepartmentSuccess>>>",data);
      return {
        ...state,
        departmentData:data,
      }
		},
		getDetailsSuccess(state,{payload}){
			const {data} = payload.response
    //   console.log("getDetailsSuccess>>>",data);
      return {
        ...state,
        DetailsData:data,
      }
		},
		getsmmcAlxrSuccess(state,{payload}){
			const {data,page} = payload.response
    //   console.log("getsmmcAlxrSuccess>>>",data);
      data.map((item)=>{
      	if(item.products){
      		item.pdtLen = item.products.length;
      	}else{
      		item.pdtLen = 0;
      	}
      })
      return {
        ...state,
        privateData:data,
        privatePage:page
      }
		},
		fundCompetitionSuccess(state,{payload}){
			const {data} = payload.response
    //   console.log("fundCompetitionSuccess>>>",data);
      return {
        ...state,
        fundGameList:data,
      }
		},
		
	}
};
